package com.jjyd.crlp.util.transform;

import java.util.LinkedList;
import java.util.List;

/**
 * list与字符串类型的转换
 */
public class ListAndStringUtil {

    /**
     * 将list转为string，如果list大小为0，返回""
     * @param list
     * @return
     */
    public static String ListToString(List<Integer> list) {
        StringBuilder stringBuilder = new StringBuilder();
        if (list == null || list.size() == 0) {
            return stringBuilder.toString();
        }
        for (Integer integer : list) {
            stringBuilder.append(integer);
            stringBuilder.append(",");
        }
        return stringBuilder.toString();
    }

    /**
     * 字符串通过逗号切割,如果字符串为空，则返回大小为0的list
     * @param s 字符串
     * @return 返回切割好的list
     */
    public static List<Integer> StringToList(String s) {
        List<Integer> list = new LinkedList<>();
        if (s == null || "".equals(s)) {
            return list;
        }
        String[] split = s.split(",");
        for (String s1 : split) {
            Integer integer = Integer.valueOf(s1);
            list.add(integer);
        }
        return list;
    }

}
